package com.baiyangtx.lingerweather.view;

import java.util.Calendar;

import com.baiyangtx.lingerweather.model.DefaultWeatherChartModel;
import com.baiyangtx.lingerweather.model.WeatherChartModel;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;




public class WC extends ViewGroup {

	
	
	private static final int NUMBER_OF_WEEK = 6 ;
	
	/*
	 * text size
	 */
	private int textSize = 25;

	/*
	 * text color 
	 */
	private int textColor = 0xFFFFFFFF;

	private int textPaddingLeft = 10;

	private int textPaddingRight = 10;

	private int textPaddingTop = 10;

	private int textPaddingBottom = 5;

	
	private int imageSize = 100 ;
	
	
	
	
	
	
	
	
	
	
	

	/*
	 * base id for children
	 */
	private int idReference = 100;
	
	/*
	 * 第一行 7天的星期数
	 */
	private TextView[] weekdays = new TextView[NUMBER_OF_WEEK];

	/*
	 * 第二行 7天的中午天气文字
	 */
	private TextView[] noonWeathersTxt = new TextView[NUMBER_OF_WEEK];

	/*
	 * 第三行 7天的中午天气图标
	 */
	private ImageView[] noonWeathersPic = new ImageView[NUMBER_OF_WEEK];

	/*
	 * 7天的夜间天气图标
	 */
	private ImageView[] nightWeathersPic = new ImageView[NUMBER_OF_WEEK];

	/*
	 * 7天的夜间天气文字
	 */
	private TextView[] nightWeathersTxt = new TextView[NUMBER_OF_WEEK];

	/*
	 * 这7天的日期
	 */
	private TextView[] days = new TextView[NUMBER_OF_WEEK];

	
	private TemperatureChart temperatureChart = null ;
	
	private WeatherChartModel model;
	
	private Context context;
	


	
	public WC( Context context, AttributeSet attrs) {
		super(context , attrs);
		this.context = context ;

		DefaultWeatherChartModel model = new DefaultWeatherChartModel(context);
		model.setToday(Calendar.getInstance());
		
		this.model = model ;
		initViews() ;
		
		updataSuface() ;
		
		
		
		
	}
	
	
	
	public void updataSuface()
	{
		for( int i = 0 ; i < model.getDaysNumber()  ; i++ )
		{
			weekdays[i].setText( model.getDayOfWeek(i) ) ;
			noonWeathersTxt[i].setText(  model.getNoonWeatherText(i) ) ;
			nightWeathersTxt[i].setText( model.getNightWeatherText(i) ) ;
			days[i].setText( model.getDate(i) );
			
			noonWeathersPic[i].setImageDrawable( model.getNoonWeatherDrawable(i) ) ;
		//	nightWeathersPic[0].setBackgroundColor(textColor) ;
			nightWeathersPic[i].setImageDrawable( model.getNightWeatherDrawable(i) ) ;
		}
		temperatureChart.setModel(model) ;
		temperatureChart.invalidate() ;
	}
	
	

	public void setWeatherChartModel(WeatherChartModel model) {
		this.model = model;
	}

	public WeatherChartModel getWeatherChartModel() {
		return model;
	}
	
	
	
	
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if( !changed){
			return ;
		}
		
		int viewHeight = textSize + textPaddingTop + textPaddingBottom ;
		int height = getMeasuredHeight() ;
		
		layoutViewInLine(weekdays, textPaddingTop , true ) ;
		layoutViewInLine(noonWeathersTxt , viewHeight , true  ) ;
		layoutViewInLine(noonWeathersPic , viewHeight * 2 , false );
		layoutViewInLine(nightWeathersPic , height - (viewHeight * 2 + textSize*3) ,false ) ;
		layoutViewInLine(nightWeathersTxt , height - viewHeight * 2 , true )  ;
		layoutViewInLine(days , height - viewHeight , true ) ;
		
		temperatureChart.layout(textPaddingLeft, 
				viewHeight*2 + textSize*3 + textPaddingTop , 
				getWidth() - textPaddingRight ,
				height - (viewHeight * 2 + textSize*3 + textPaddingBottom) ) ;
		
	}
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int width = measureWidth(widthMeasureSpec) ;
		int height = measureHeight(heightMeasureSpec) ;
		
		setMeasuredDimension(width, height) ;
		
	}
	
	
	
	
	
	private void layoutViewInLine(View[] views , int t  , boolean padding ){
		int blank = getWidth()/ NUMBER_OF_WEEK ;
		for( int i = 0 ; i < views.length ; i++ ){
			int left = blank * i + textPaddingLeft ;
			int right = left + textSize * 3 ;
			
			int top = t ;
			int bottom = top + textSize + textPaddingTop + textPaddingBottom ;
			
			if( padding ){
				left += textPaddingLeft ;
				right += textPaddingRight ;
			}else{
				bottom = top + textSize*3 ;
			}
			
			views[i].layout(left, top, right, bottom) ;
		}
		
	}
	
	
	
	
	
	
	
	
	
	

	
	
	
	private void addViews()
	{
		for (int i = 0; i < weekdays.length; i++) {
			addView(weekdays[i]);
			addView(noonWeathersTxt[i]);
			addView(noonWeathersPic[i]);
			
			addView(days[i]);
			addView(nightWeathersPic[i]);
			addView(nightWeathersTxt[i]);
		}
		
		addView(temperatureChart) ;
	}
	
	
	
	
	private void initViews() {
		for (int i = 0; i < weekdays.length; i++) {
			weekdays[i] = new TextView(context);
			weekdays[i].setId(idReference + i);
			weekdays[i].setTextSize(textSize) ;
			weekdays[i].setTextColor(textColor) ;

			noonWeathersTxt[i] = new TextView(context);
			noonWeathersTxt[i].setId(idReference + NUMBER_OF_WEEK * 1
					+ i);
			noonWeathersTxt[i].setTextSize(textSize);
			noonWeathersTxt[i].setTextColor(textColor) ;

			noonWeathersPic[i] = new ImageView(context);
			noonWeathersPic[i].setId(idReference + NUMBER_OF_WEEK * 2
					+ i);

			nightWeathersPic[i] = new ImageView(context);
			nightWeathersPic[i].setId(idReference + NUMBER_OF_WEEK * 3
					+ i);

			nightWeathersTxt[i] = new TextView(context);
			nightWeathersTxt[i].setId(idReference + NUMBER_OF_WEEK * 4
					+ i);
			nightWeathersTxt[i].setTextSize(textSize) ;
			nightWeathersTxt[i].setTextColor(textColor) ;

			days[i] = new TextView(context);
			days[i].setId(idReference + NUMBER_OF_WEEK * 5 + i);
			days[i].setTextSize(textSize) ;
			days[i].setTextColor(textColor) ;
		}
		temperatureChart = new TemperatureChart(context) ;
		temperatureChart.setId(idReference + NUMBER_OF_WEEK * 6) ;
		temperatureChart.setModel(model) ;
		
		addViews() ;
	}
	
	

	
	
	private int measureHeight( int measureSpec ){
		int size  =  MeasureSpec.getSize(measureSpec) ;
		int model = MeasureSpec.getMode(measureSpec);
		int height = (textSize + textPaddingTop + textPaddingBottom)*4 + imageSize*2   ;
		if( model == MeasureSpec.EXACTLY ){
			
		}else if( model == MeasureSpec.AT_MOST ){
			if( size < height ){
				
			}else{
				size = height ;
			}
		}else if( model == MeasureSpec.UNSPECIFIED ){
			size = height ;
		}
		
		return size ;
	}
	
	
	private int measureWidth( int measureSpec ){
		int size  =  MeasureSpec.getSize(measureSpec) ;
		int model = MeasureSpec.getMode(measureSpec);
		int width = (textSize * 3 + textPaddingLeft + textPaddingRight) * NUMBER_OF_WEEK ;

		if( model == MeasureSpec.EXACTLY ){
			
		}else if( model == MeasureSpec.AT_MOST ){
			if( size < width ){
				
			}else{
				size = width ;
			}
		}else if( model == MeasureSpec.UNSPECIFIED ){
			size = width ;
		}
		return size ;
	}
	
}
